﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using GameBase;

namespace GalaxyPresident
{
    public class SolarLayer : PlayScreenLayer
    {
        SolarSystem solarSystem;
        InfomationType defaultInfoType = InfomationType.None;

        public InfomationType DefaultInfoType
        {
            get { return defaultInfoType; }
            set { defaultInfoType = value; }
        }
        public SolarSystem SolarSystem
        {
            get { return solarSystem; }
            set { solarSystem = value; }
        }
        List<PlanetView> listPlanetViews;
        PlanetView selectingPlanetView;
        
        public SolarLayer(PlayingScreen playingScreen, Planet planet)
            : base(playingScreen)
        {
            layerMotion.Friction = 5;
            topLeft = new Vector2(-400, -500);
            bottomRight = new Vector2(400, 500);
            margin = new Vector2(200, 200);
            minScale = 0.5f;
            scaleSpeed = 0.95f;
            scaleRate = 0.7f;
            //lastMouseState = Mouse.GetState();
            backgroundLayer = new Perspective3DGroup(Game.GraphicsDevice.Viewport, 200);

            for (int i = 0; i < 50; i++)
            {
                int b = 1000;
                int a = 1200;
                int x = Constant.random.Next(2 * a) - a;
                int y = Constant.random.Next(b * 2) - b;

                Visble3DObject star = new Visble3DObject(@"Resources/star", 1, 3,
                    new Vector3(x, y, Constant.random.Next(400) - 600));
                star.Scale = 0.6f;
                star.Sprite.FrameSequence = new List<int> { 0, 1, 2, 1 };
                star.Sprite.CurrentFrame = Constant.random.Next(4);
                star.Sprite.Delay = Constant.random.Next(50) + 50;
                backgroundLayer.AddChild(star);
            }
            Vector2 center = new Vector2(Constant.screenWidth, Constant.screenHeight) / 2;
            backgroundLayer.Translate(center.X, -center.Y);
            
            Layer = new VisibleGroup();


            this.SetSolarSystem(planet.SolarSystem,false);
            //Layer.Position = -planet.Position;
            
        }
        Perspective3DGroup backgroundLayer;
        public void SetSolarSystem(SolarSystem solarSys,bool zoomIn)
        {

            listPlanetViews = new List<PlanetView>();
            this.RemoveAllChild();
            //lastMouseState = Mouse.GetState();
            SolarSystem = solarSys;

            Layer.RemoveAllChild();
            Vector2 center = new Vector2(Constant.screenWidth, Constant.screenHeight) / 2;
            Position = center;
            VisibleObject halo = new VisibleObject(@"Resources/halo", 1, 1);
            halo.Depth = 0.12f;
            halo.Scale = 2 * solarSys.Planets.Count / (float)4;
            halo.Sprite.SetOriginPosition(ORIGIN_POSITION.Center);
            halo.Color = Color.Lerp(SolarSystem.Color,Color.White,0.5f);
            Layer.AddChild(halo);
            Layer.AddChild(new SunView(solarSys));
            for (int i = 0; i < 30; i++)
            {
                float random = Constant.random.Next(30);
                float a = (float)(MathHelper.TwoPi * Constant.random.NextDouble());
                float r = 1500 - random * random;
                int x = (int)(Math.Sin(a) * r);
                int y = -(int)(Math.Cos(a) * r);

                VisibleObject star = new VisibleObject(@"Resources/nebula2", 1, 1,
                    new Vector2(x, y));
                star.Scale = (float)(1 + r / 1500);
                star.Rotate = a;
                star.Depth = 0.1f;
                star.Anpha = 0.5f;
                star.Sprite.SetOriginPosition(ORIGIN_POSITION.Center);
                Layer.AddChild(star);
            }
            foreach (Planet planet in solarSys.Planets)
            {
                PlanetView planetView = new PlanetView(planet);
                listPlanetViews.Add(planetView);
                Layer.AddChild(planetView);
                Layer.AddChild(planet.OrbitPlanet);
                planet.OrbitPlanet.Visible = false;
            }
            Layer.AddChild(backgroundLayer);
            AddChild(Layer);
            Layer.Position = Vector2.Zero;
            TargetScale = 0.8f;
            if (zoomIn)
            {
                Scale = 0.2f;
            }
            else
            {
                Scale = 2f;
            }
        }
        public void ResetView()
        {
            //lastMouseState = Mouse.GetState();
            mousePressed = false;
            Scale = 2f;
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            base.Draw(gameTime, spriteBatch);
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            foreach (Planet planet in SolarSystem.Planets)
            {
                if (Scale <= minScale)
                {
                    planet.OrbitPlanet.Visible = false;
                    
                }
                else
                {
                    planet.OrbitPlanet.Visible = true;
                }
            }
            if (selectingPlanetView != null)
            {
                if (Scale < 2)
                {
                    Scale *= 1.1f;
                    Layer.Position = MotionEffects.Move(Layer.Position, -selectingPlanetView.Position, 40);
                }
                else
                {
                    _playingScreen.ShowPlanetLayer(selectingPlanetView.Planet);
                    mousePressed = false;
                    selectingPlanetView = null;
                    TargetScale = 0.5f;
                }
            }
        }
        public override void MouseReleased(bool onForm)
        {
            base.MouseReleased(onForm);
            foreach (PlanetView planetView in listPlanetViews)
            {
                if (planetView.IsHover(MouseState.X, MouseState.Y, planetView.getBound()) &&
                    MouseState.X == pressedPoint.X && MouseState.Y == pressedPoint.Y)
                {
                    selectingPlanetView = planetView;
                }
            }
        }
        public override void MouseHandle()
        {
            base.MouseHandle();

            foreach (PlanetView planetView in listPlanetViews)
            {
                if (planetView.IsHover(MouseState.X, MouseState.Y, planetView.getBound()))
                {
                    planetView.ShowInfo = InfomationType.Full;
                }
                else
                {
                    planetView.ShowInfo = defaultInfoType;
                }
            }
            
        }
    }
}
